<?php

class Service_LoanController extends Custom_Controller_Action {
    public function indexAction() {
        $customerId = $this->_getParam('cid');
        $objLoan = new Service_Model_DbTable_Loan();
        $page = $this->getRequest()->getParam('page');
        $objLoan->setPaginator($this->Config->params->recordsPerPage,$page);
        $loans = $objLoan->getByColumn('customer_id' ,$customerId);
        //echo '<pre>';print_r($loans);die;
        $this->view->loans = $loans;
        $this->view->paginator = $objLoan->getPaginator();
        $this->view->page = $page;
    }

    public function addAction() {
        $cid = $this->_getParam('cid');
        $this->view->cid = $cid;

        if($cid) {
            $id = Custom_General::decode($cid);

            $values = array();
            $form = new Service_Form_Loan();
            $this->view->form = $form;

            if($this->getRequest()->isPost()) {
                $objService	= new Service_Model_DbTable_Loan();
                $formData = $this->getRequest()->getPost();

                if($form->isValid($formData)) {

                    $array1['customer_id'] = $id;
                    $array2 = $form->getValues();
                    $values = array_merge($array1, $array2);

                    $newId = $objService->insert($values);

                    if($newId) {
                        $this->addMessage("Loan added successfully");
                        $this->RedirectUrl("customer/add");
                    }
                    else {
                        $this->addMessage("Loan not added successfully", 'error');
                    }
                }
            }
        }
    }

    public function editAction() {
        $lid = $this->_getParam('lid');
        $this->view->lid = $lid;
        if($lid) {
            $form = new Service_Form_Loan();
            $id = Custom_General::decode($lid);
            $objLoan = new Service_Model_DbTable_Loan();

            if($this->getRequest()->isPost()) {
                $formData = $this->getRequest()->getPost();

                if($form->isValid($formData)) {
                    $values = $form->getValues();
                    $res = $objLoan->updateById($values, $id);

                    if($res) {
                        $this->addMessage("Loan information updated successfully");
                        $this->RedirectUrl($this->getRequest()->getServer('HTTP_REFERER'));
                    }
                    else {
                        $this->addMessage("Loan information not updated successfully", 'error');
                    }
                }
            }

            $loan = $objLoan->getOne($id);
            if($loan) {
                $form->populate($loan);
                $this->view->form = $form;
            }
        }

    }

    public function deleteAction() {
        $lid = $this->_getParam('lid');

        if($lid) {
            $objLoan = new Service_Model_DbTable_Loan();
            $id = Custom_General::decode($lid);
            $res = $objLoan->deleteById($id);

            if($res)
                $this->addMessage('Loan delete successfully');
            else
                $this->addMessage('Loan not delete successfully','error');

            if($this->_getParam('page'))
                $params = array('page'=>$this->_getParam('page'));
            else
                $params = array();

            $this->RedirectUrl('customer/show/',$params);
        }
    }
}

